<?php

$id = empty($this->Param['id']) ? 0 : $this->Param['id'];
$this->Assign('id', $id);

$this->LoadHelper('DealersHelper');
$DealersHelper = new DealersHelper();

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('BranchHelper');
$BranchHelper = new BranchHelper();

if (empty($this->Param['option'])) {
    $data = $DealersHelper->dealersRow(array('`dl_id` = ?' => $id, '`dl_status` = ?' => 'ok'));
    $this->Assign('data', $data);
    $branch = $BranchHelper->branchAll(array('`br_status` = ?' => 'ok'));
    $this->Assign('branch', $branch);
    echo $this->GetView('edit.php');
} else {
    switch ($this->Param['option']) {
        case 'save':
            if($id > 0){
                $result = $DealersHelper->dealersSave($this->Param, array('`dl_id` = ?' => $id, '`dl_status` = ?' => 'ok'));
            }else{
                $result = $DealersHelper->dealersSave($this->Param);
            }
            if($result['success']){
                $desc = $id > 0 ? '修改经销商信息' : '新增经销商信息';
                $MemberHelper->saveLog($this->UserConfig, $desc, json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', TRUE);
            }
            ErrorMsg::Debug($result['msg']);
            break;
        case 'remove':
            $id = empty($this->Param['id']) ? ErrorMsg::Debug('参数错误') : $this->Param['id'];
            $result = $DealersHelper->dealersRemove(array('`dl_id` = ?' => $id, '`dl_status` = ?' => 'ok'));
            if($result){
                $MemberHelper->saveLog($this->UserConfig, '删除经销商', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('删除成功');
            }
            ErrorMsg::Debug('删除失败');
            break;
        default:
    }
    
}